import dva from 'dva';
import {routerRedux} from 'dva/router'
import {call, put} from 'dva/saga'
import {message} from 'antd'
// import {reqwest_get,reqwest_url} from '../../utils/Reqwest.js'
import * as registServices from '../../services/Login/Index.js'
// import axios from 'axios'

// const delay =timeout => new Promise(resolve => setTimeout(resolve, timeout))
let _data = [];
// let token = window.localStorage.getItem('token').
const delay = setTimeout(timeout  => new Promise(resolve => setTimeout(resolve, timeout)));


export default {
    namespace: 'regist',
    state: {
    	list: [],
    	loading: false,
    	table: [],
    	page: 'first'
    },
    reducers: {
	    codeServer(state, { payload }) {
            console.log(payload)
            // message.success('发送成功')
            if(payload.data.status === 'success'){
                message.success('发送成功')
            }else{
                message.error(payload.data.msg)
            }
	        return {...state}
	      
	    },
	    loading(state, { payload }) {
	    	return{loading: true}
	    },
	    editStore(state, { payload }) {
	    	console.log('我的')
	    	return{...state,table: payload.payload}
	    },
	    editSave(state, { payload }) {
	    	message.success('更新成功')
	    	return{...state,table: payload.payload.table , loading: false}
	    },
        nextPage(state, { payload }) {
            console.log(payload.payload)
            return{...state,page: 'post' ,payload: payload.payload}
        },
    },
    effects:{
    	*first ({payload },{put,call}) {
    		// const data = yield call(ordersServices.test, { payload });
    		yield put({type: 'editStore'}) 

    	},
    	*code ({payload },{put,call}) {
    		// yield put({type: 'loading'})
    		const { data, headers } = yield call(registServices.code, { payload });
    		// if(data.status ==='success'){
    		yield put({type: 'codeServer', payload:{payload, data}}) 
    		// }
    	},
    	*nextStep ({payload },{put,call}) {
    		// const { data, headers } = yield call(ordersServices.listSave, { payload });
    		yield put({type: 'nextPage', payload:{payload}}) 
            // yield call(delay, 1000)
    	},
    	*createUser ({payload },{put,call}) {
    		const { data, headers } = yield call(ordersServices.listSave, { payload });
    		if(data.status ==='success'){
    			yield put({type: 'editSave', payload:{payload}}) 
    		}
    	},
    },
    subscriptions: {
    	setup({history, dispatch}) {
    		// console(10000)
    		return history.listen(({pathname}) => {
    			// console.log(pathname)
    			if(pathname === '/regist'){
    				// dispatch({ type: 'fetch'})
    			} 
    		})
    	}
    }
  
};
